<%@page import="cn.yi.newsys.pojo.News"%>
<%@page import="cn.yi.newsys.service.impl.CommentServiceImpl"%>
<%@page import="cn.yi.newsys.service.CommentService"%>
<%@page import="cn.yi.newsys.pojo.Comment"%>
<%@page import="cn.yi.newsys.pojo.User"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'doComment.jsp' starting page</title>

  </head>
  
  <body>
    <%
    	// 创建评论业务逻辑对象
    	CommentService cs = new CommentServiceImpl() ;
    	// 获取会话中的用户对象
    	User user = (User)session.getAttribute("User") ;
    	// 获取请求中的评论
    	String comment = request.getParameter("comment") ;
    	// 获取新闻ID
    	News n = (News)session.getAttribute("news");
    	int cnid = n.getNid() ;
    	// 创建日期对象
    	Date date = new Date() ;
    	// 获取 IP 地址
    	String ip = request.getRemoteAddr();
    	if(user != null){
    		// 创建评论对象
    		Comment com = new Comment(null,cnid,comment,date,ip,user.getUserName()) ;	
    		// 保存评论
    		boolean flag = cs.save(com) ;
    		if(flag){
    			out.print("<script>alert('评论成功');location.href='../newsread.jsp?id="+cnid+"';</script>");
    		}else{
    			out.print("<script>alert('出现未知原因，评论失败！');location.href='../index.jsp';</script>");
    		}
    	}else{
    		out.print("<script>alert('未登录用户，不能发表评论！');location.href='../index.jsp';</script>");
    	}
    
     %>
  </body>
</html>
